#include <gtk/gtk.h>
#include <string.h>
#include "id3e.h"
#include "callback.h"
#include "list.h"
#include "gva.h"

G_MODULE_EXPORT
void on_about_activate (GtkMenuItem *menuitem, Id3e *id3e)
{
	static const gchar * const authors[] = {
		"Lyre <4179e1@gmail.com>",
		NULL
	};

	static const gchar *license = \
		"               ID3 Editor\n"
		"    Copyright (C) <2010>  <Lyre>\n"
		"    \n"
		"    This program is free software: you can redistribute it and/or modify\n"
		"    it under the terms of the GNU General Public License as published by\n"
		"    the Free Software Foundation, either version 3 of the License, or\n"
		"    (at your option) any later version.\n"
		"    \n"
		"    This program is distributed in the hope that it will be useful,\n"
		"    but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
		"    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
		"    GNU General Public License for more details.\n"
		"    \n"
		"    You should have received a copy of the GNU General Public License\n"
		"    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n"
		;

	static const gchar copyright[] = \
		"Copyright \xc2\xa9 2010 Lyre";

	static const gchar comments[] = "ID3 Tag Information Editor";

	gtk_show_about_dialog (GTK_WINDOW (id3e_get_window (id3e)),
			       "authors", authors,
				   "license", license,
			       "comments", comments,
			       "copyright", copyright,
#ifdef HAVE_CONFIG_H
			       "version", VERSION,
#endif /* HAVE_CONFIG_H */
			       "website", "http://code.google.com/p/id3e/",
			       "program-name", _("ID3 Editor"),
			       "logo-icon-name", GTK_STOCK_EDIT,
			       NULL); 
}

G_MODULE_EXPORT 
void on_sidebar_toggle (gpointer *widget, Id3e *id3e)
{
	id3e_sidebar_toggle (id3e);
}

G_MODULE_EXPORT 
void on_statusbar_toggle (gpointer *widget, Id3e *id3e)
{
	id3e_statusbar_toggle (id3e);
}

G_MODULE_EXPORT
void on_secondary_icon_press (GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, Id3e *id3e)
{
	if (icon_pos == GTK_ENTRY_ICON_SECONDARY)
	{
		/* clear text */
		gtk_entry_set_text (entry, "");
	}
}

G_MODULE_EXPORT
void on_edit_clicked (GtkButton *button, Id3e *id3e)
{
	Gva *gva = id3e_get_gva (id3e);
	if (id3e_list_get_selected_count (id3e) == 0)
	{
		gva_set_sensitive (gva, FALSE);
		return;
	}
	gva_set_sensitive (gva, !gva_is_sensitive (gva));
}

G_MODULE_EXPORT
void on_save_clicked (GtkButton *button, Id3e *id3e)
{
	gint selected_num;
	Gva *gva;
	Enc *enc;

	selected_num = id3e_list_get_selected_count (id3e);
	gva = id3e_get_gva (id3e);
	enc = id3e_get_enc (id3e);

	if (selected_num == 0) /* no item selected */
	{
		return;
	}
	else	/* selected one or more items */
	{
		GList *list;
		GList *ptr;
		GtkTreeModel *model;
		GtkTreeIter iter;
		gchar *path = NULL;
		gchar *src;
		gboolean result;
		Id3 *tag;
		Id3 *tag_new;

		src = enc_get_src_text (enc);
		model = GTK_TREE_MODEL (id3e_get_list_store (id3e));
		list = id3e_list_get_selected_rows (id3e);
		ptr = list;
		tag = id3_new ();

		gva_write_to_id3_multi (gva, tag);
		tag_new = id3_multi_convert (tag, src, "UTF-8", &result);

		for (ptr = list; ptr != NULL; ptr = g_list_next (ptr))
		{
			if (gtk_tree_model_get_iter (model, &iter, (GtkTreePath *)ptr->data))
			{
				gtk_tree_model_get (model, &iter, 1, &path, -1);
				id3_multi_write_to_path (tag_new, path);
				g_free (path);
			}
		}
		id3_free (tag_new);

		if (!result)
		{
			id3e_statusbar_message (id3e, "%s Convert from UTF-8 to %s fail", __func__, src);
		}

		id3_free (tag);

		g_free (src);
		g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
		g_list_free (list);
	}
}

G_MODULE_EXPORT
void on_enc_changed (GtkComboBox *combo, Id3e *id3e)
{
	gint index;
	if ((index = gtk_combo_box_get_active (combo)) == -1)
	{
		return;
	}
	on_selection_changed (id3e_list_get_selection (id3e) , id3e);
}

G_MODULE_EXPORT
void on_enc_activate (GtkEntry *entry, Id3e *id3e)
{
	on_selection_changed (id3e_list_get_selection (id3e) , id3e);
}

G_MODULE_EXPORT
void on_convert_clicked (GtkButton *button, Id3e *id3e)
{
	gint selected_num;
	Gva *gva;
	Enc *enc;

	selected_num = id3e_list_get_selected_count (id3e);
	gva = id3e_get_gva (id3e);
	enc = id3e_get_enc (id3e);

	if (selected_num == 0)
	{
		return;
	}
	else
	{
		GList *list;
		GList *ptr;
		GtkTreeModel *model;
		GtkTreeIter iter;
		gchar *path = NULL;
		gchar *src;
		gchar *dest;
		gboolean result;

		src = enc_get_src_text (enc);
		dest = enc_get_dest_text (enc);
		model = GTK_TREE_MODEL (id3e_get_list_store (id3e));
		list = id3e_list_get_selected_rows (id3e);

		GvaSelection select;
		if ((select = gva_get_selection (gva) )== GVA_SELECTION_V1)
		{
			for (ptr = list; ptr != NULL; ptr = g_list_next (ptr))
			{
				if (gtk_tree_model_get_iter (model, &iter, (GtkTreePath *)ptr->data))
				{
					gtk_tree_model_get (model, &iter, 1, &path, -1);
					id3v1_convert_path (path, dest, src, &result);

					g_free (path);
				}
			}
		}
		else if (select == GVA_SELECTION_V2)
		{
			g_message ("nothing to do with Id3v2 now");
		}
		else
		{
			g_warning ("Criticual error! Should not reach here.");
		}

		if (!result)
		{
			id3e_statusbar_message (id3e, _("Error occur while converting from %s to %s"), src, dest);
		}

		/* swap src/dest codeset */
		enc_set_src_text (enc, dest);
		enc_set_dest_text (enc, src);

		on_selection_changed (id3e_list_get_selection (id3e), id3e);

		g_free (src);
		g_free (dest);

		g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
		g_list_free (list);
	}

}

G_MODULE_EXPORT
void on_switch_page (GtkNotebook *notebook, GtkNotebookPage *page,
		guint page_num, Id3e *id3e)
{
	Gva *gva;

	gva = id3e_get_gva (id3e);
	gva_set_selection (gva, (GvaSelection)page_num);
}
